package com.jd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jd.common.E3Result;
import com.jd.common.Page;
import com.jd.pojo.Admin;
import com.jd.pojo.Category;
import com.jd.pojo.Movie;
import com.jd.pojo.Query;
import com.jd.service.LoginService;
import com.jd.service.MovieService;
import com.jd.vo.NewMovie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MovieController {


    @Autowired
    private LoginService loginService;

    @Autowired
    private MovieService movieService;

    /**
     * 测试test
     * @return
     */
    @RequestMapping("/test")
    @ResponseBody
    public JSONObject test(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("result","success");
        jsonObject.put("result","success");
        return jsonObject;
    }

    /**
     * 返回登录页面
     * @return
     */
    @RequestMapping("/")
    public String index(){
        return "login";
    }

    /**
     *
     */
    @RequestMapping("/login")
    @ResponseBody
    public E3Result login(Admin admin, HttpSession session){

        System.out.println("账号密码：" + JSON.toJSONString(admin));
        E3Result result = loginService.checkUserNameAndPasswd(admin,session);
        return result;

    }

    @RequestMapping("/movie")
    public String showMovie() {
        return "redirect:/movie/list";
    }

    @RequestMapping("/movie/list")
    public String list(Query query, Model model){

        Page<NewMovie> movies = movieService.selectMovieList(query);
        model.addAttribute("page", movies);

        List<Category> categorylist = movieService.selectCategory();
        model.addAttribute("categoryList", categorylist);

        //参数回显
        model.addAttribute("movieName", query.getMovieName());
        model.addAttribute("categoryId", query.getCategoryId());
        return "movieManage";
    }

    @RequestMapping("/movie/delete")
    @ResponseBody
    public String deleteUser(Integer id) {

        movieService.deleteMovie(id);
        return "ok";
    }


    /**
     * 回显电影信息
     * @param id
     * @return
     */
    @RequestMapping("/movie/edit")
    @ResponseBody
    public NewMovie getMovieById(Integer id) {
        NewMovie newMovie = movieService.selectMovieById(id);
        return newMovie;
    }


    @RequestMapping("/movie/update")
    @ResponseBody
    public String updateMovie(Movie movie, HttpServletRequest request) {
        String[] categoryIds = request.getParameterValues("categoryId");
        movieService.updateMovie(movie, categoryIds);
        return "OK";
    }



    @RequestMapping("/movie/add")
    @ResponseBody
    public String addMovie(Movie movie, HttpServletRequest request) {
        String[] categoryIds = request.getParameterValues("categoryId");
        System.out.println("movie： " + JSON.toJSONString(movie));
        System.out.println("categoryIds： " + JSON.toJSONString(categoryIds));
        movieService.addMovie(movie, categoryIds);
        return "OK";
    }


}
